package controller

import (
	"github.com/gofiber/fiber"
	jsoniter "github.com/json-iterator/go"
	"gowork/performanceSharing/model"
	"strconv"
)

type PerformanceSharingController struct {
	ShdaingRecords *model.ShdaingRecords
}

func (psc *PerformanceSharingController)ShdaingPerformance(c *fiber.Ctx){

	psc.ShdaingRecords = new(model.ShdaingRecords)

	orderIdStr := c.FormValue("orderId")
	orderId, err := strconv.ParseInt(orderIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	operatorIdStr := c.FormValue("operatorId")
	operatorId, err := strconv.ParseInt(operatorIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	var shdaingRecordList []*model.Shdaing
	shdaingRecordListStr := c.FormValue("shdaingRecordList")
	uErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(shdaingRecordListStr), &shdaingRecordList)
	if uErr != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": uErr.Error(),
		})
		return
	}

	shdaingType := c.FormValue("shdaingType")

	res , err := psc.ShdaingRecords.ShdaingPerformance(orderId, operatorId, shdaingRecordList, shdaingType)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}